<?php
namespace app\home\controller;

use \app\common\controller\Homebase;
use \thans\Ret;
use \think\Loader;
use \think\Request;

class User extends Homebase
{
    public function _initialize()
    {
        $this->user = Loader::model('User');
        $this->auth = Loader::model('Auth');
        /**
         * 管理员用户三级菜单
         */
        $this->umenu[0]['text'] = '管理员列表';
        $this->umenu[0]['url']  = 'index';
        $this->umenu[1]['text'] = '新增管理员';
        $this->umenu[1]['url']  = 'edit';
        /**
         * 会员三级菜单
         */
        $this->mmenu[0]['text'] = '会员列表';
        $this->mmenu[0]['url']  = 'member';
        $this->mmenu[1]['text'] = '新增会员';
        $this->mmenu[1]['url']  = 'edit_member';

    }
    public function index()
    {
        $this->assign('third_menu', $this->umenu);
        $list = $this->user->where('is_del', 0)->where('user_type', 0)->paginate(10);
        $this->assign('list', $list);
        return $this->fetch();
    }
    /**
     * 会员列表页
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function member()
    {
        $this->assign('third_menu', $this->mmenu);
        $list = $this->user->where('is_del', 0)->where('user_type', 1)->paginate(10);
        $this->assign('list', $list);
        return $this->fetch();
    }
    /**
     * 编辑、添加会员
     * @return [type] [description]
     */
    public function edit_member()
    {
        $uid   = input('uid');
        if ($uid == 1) {
            die('非法操作');
        }
        if ($uid) {
        	$edit_res=$this->user->getUserById($uid);
            $this->assign('data', $edit_res);
        }
        if (IS_POST) {
        	$res  = $this->user->saveUserInfo(input('post.'),$uid);
        	if(!$uid){
        		if ($res['code']) {
                    return $this->ret->setCode(0)->setAsk("新增成功,是否继续？")->toJson();
                } else {
                    return $this->ret->setCode(1)->setMsg($res['msg'])->toJson();
                }
        	}else{
        		if ($res['code']) {
                    return $this->ret->setCode(0)->setMsg('更新成功')->setBack()->toJson();
                } else {
                    return $this->ret->setCode(1)->setMsg($res['msg'])->toJson();
                }
        	}
        }
        $this->assign('third_menu', $this->mmenu);
        return $this->fetch();
    }
    //修改用户状态
    public function change_status()
    {
        $uid = input('uid');
        if ($uid == 1) {
            return $this->ret->setCode(1)->setMsg('禁止修改超级管理员状态!')->toJson();
        }
        $data['status'] = input('status');
        $res            = $this->user->where('user_id', $uid)->update($data);
        if ($res) {
            if ($data['status'] == 0) {
                $status = "停用";
            } else {
                $status = "启用";
            }
            return $this->ret->setCode(0)->setMsg($status)->toJson();
        } else {
            return $this->ret->setCode(1)->setMsg('修改状态失败')->toJson();
        }
    }
    /*
    根据用户删除用户
     */
    public function del()
    {
        $uid = input('uid');
        if ($uid == 1) {
            return $this->ret->setCode(1)->setMsg('非法操作')->toJson();
        }
        $res = $this->user->where('user_id', $uid)->find();
        if (!$res || $res['is_del']) {
            return $this->ret->setCode(1)->setMsg('该用户不存在')->toJson();
        }
        $data['is_del'] = '1';
        $res            = $this->user->where('user_id', $uid)->update($data);
        if ($res) {
            return $this->ret->setCode(0)->setMsg('删除用户成功')->setReload()->toJson();
        } else {
            return $this->ret->setCode(1)->setMsg('删除用户失败')->toJson();
        }
    }
    /**
     * 用户编辑—添加（管理员）
     */
    public function edit()
    {
        $uid = input('uid');
        if ($uid == 1) {
            die('非法操作');
        }
        if (IS_POST) {
            if (input('user_pwd')) {
                $rand                  = inge_get_random_str(10);
                $data['user_pwd_rand'] = $rand;
                $data['user_pwd']      = inge_encryption_pwd(input('user_pwd'), $rand);
            }
            $data['user_name'] = input('user_name');
            $data['auth_id']   = input('auth_id');
            if (input('?status')) {
                $data['status'] = '1';
            } else {
                $data['status'] = '0';
            }
            $data['email'] = input('email');
            if ($uid) {
                $data['update_date'] = date('Y-m-d H:i:s', time());
                $res                 = $this->user->where($data)->find();
                if ($res) {
                    return $this->ret->setCode(0)->setMsg('修改成功')->setBack()->toJson();
                } else {
                    $res = $this->user->where('user_id', $uid)->update($data);
                    $msg = "修改";
                }
            } else {
                if (!input('user_pwd')) {
                    return $this->ret->setCode(1)->setMsg('必须输入密码')->toJson();
                }
                $data['create_date'] = date('Y-m-d H:i:s', time());
                $data['create_ip']   = inge_get_ip();
                $res                 = $this->user->insert($data);
                $msg                 = "添加";
            }
            if ($res) {
                return $this->ret->setCode(0)->setMsg($msg . '成功')->setBack()->toJson();
            } else {
                return $this->ret->setCode(1)->setMsg($msg . '失败')->toJson();
            }
        }
        if ($uid) {
            $res = $this->user->getUserById($uid);
            $this->assign('data',$res);
            $select = $res['auth_id'];
        } else {
            $select = 0;
        }
        $this->assign('third_menu', $this->umenu);
        $list = $this->auth->select();
        $tree = $foo = new \thans\Common\tree($list);
        $tree->formatArrayForTree('auth_id', 'body_id', 'auth_name');
        $data = $tree->get_tree(0, "<option value='\$auth_id' \$selected>\$spacer \$auth_name </option>", $select);
        $this->assign('authlist', $data);
        return $this->fetch();
    }
}
